package com.gpay.erp.services.purchase.impl;

import com.gpay.erp.core.dao.BaseDao;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.services.common.SystemCommonService;
import com.gpay.erp.services.common.impl.StringUtil;
import com.gpay.erp.services.purchase.ContractService;
import com.gpay.erp.services.purchase.PaidApplicationService;
import com.gpay.erp.services.purchase.PaidApplicationSubService;
import com.gpay.erp.services.purchase.bean.Contract;
import com.gpay.erp.services.purchase.bean.PaidApplication;
import com.gpay.erp.services.purchase.bean.PaidApplicationSub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * Created by wuzonghui on 15/11/6.
 */
@Service
public class PaidApplicationServiceImpl implements PaidApplicationService {
    @Resource
    private BaseDao dao;
    @Autowired
    private SystemCommonService systemCommonService;
    @Autowired
    private PaidApplicationSubService paidApplicationSubService;
    @Autowired
    private ContractService contractService;

    @Override
    public int insert(PaidApplication paidApplication) {
        return dao.insert("paidApplication.insert",paidApplication);
    }

    @Override
    public int delete(PaidApplication paidApplication) {
        return 0;
    }

    @Override
    public int deletes(String[] ids) {
        return 0;
    }

    @Override
    public int update(PaidApplication paidApplication) {
        return dao.update("paidApplication.update",paidApplication);
    }

    @Override
    public PaidApplication selectOne(PaidApplication paidApplication) {
        return null;
    }

    @Override
    public PaidApplication selectById(String id) {
        return (PaidApplication) dao.selectOne("paidApplication.selectById",id);
    }

    @Override
    public PaidApplication selectByFid(String f_paymentOrderId) {
        return (PaidApplication) dao.selectOne("paidApplication.selectByFid",f_paymentOrderId);
    }
    @Override
    public PagerModel selectPageList(PaidApplication paidApplication) {
        return dao.selectPageList("paidApplication.selectPageList","paidApplication.selectPageCount",paidApplication);
    }

    @Override
    public List<PaidApplication> selectList(PaidApplication paidApplication) {
        return null;
    }

    @Override
    public String getPaidApplicationCode() {

        String strCode = systemCommonService.saveTwoSelectByName("PAID003");
        return strCode;
    }
    /**
     * 添加保存付款申请状态
     * @param paidApplication 主表
     * @param paidApplicationSubList 字表
     * @param contractList 合同主表
     * @return
     */
    @Override
    public int savePaidApplicationSubList(PaidApplication paidApplication, List<PaidApplicationSub> paidApplicationSubList, List<Contract> contractList) {
        int insertV = 1;
        insertV = insert(paidApplication);
        int counts = 1;
        if(insertV !=1){
            for(Contract contract:contractList) {  //修改合同主表信息
                contractService.update(contract);
            }
            for (PaidApplicationSub paidApplicationSub:paidApplicationSubList) {
                paidApplicationSub.setF_paidId(String.valueOf(insertV));
                paidApplicationSub.setPaidAppNum(paidApplication.getPaidAppNumber());
                paidApplicationSub.setPadAppSub(paidApplication.getPaidAppNumber()+"00"+counts);
                paidApplicationSub.setPaidAmount(0.00);
                paidApplicationSubService.insert(paidApplicationSub);
                counts++;
            }
        }

        return insertV;
    }

    /**
     * 修改保存付款申请子项
     * @param paidApplication
     * @param paidApplicationSublist
     * @return
     */
    @Override
    public int saveUpdatePaidApplicationSub(PaidApplication paidApplication, List<PaidApplicationSub> paidApplicationSublist, List<Contract> contractList1,List<Contract> contractList) {
        int updateId = 1;
        updateId = update(paidApplication);
        if (updateId != 1) {
            for(Contract contract1:contractList1) {  //还原付款申请之前关联的合同申请中金额
                contractService.update(contract1);
            }
            for(Contract contract:contractList) {  //修改合同主表信息
                contractService.update(contract);
            }
            for (PaidApplicationSub paid:paidApplicationSublist) {
                if("".equals(paid.getId())||null == paid.getId())
                {
                    paid.setF_paidId(paidApplication.getId());
                    paid.setPaidAmount(0.00);
                    int counts = paidApplicationSubService.selectList(paid).size();
                    counts++;
                    paid.setF_paidId(paidApplication.getId());
                    paid.setPadAppSub(paidApplication.getPaidAppNumber()+"00"+counts);
                    paidApplicationSubService.insert(paid);
                }
                else {
                    paid.setPaidAmount(0.00);
                    paidApplicationSubService.update(paid);
                }
            }
        }
        return updateId;
    }
    /**
     * 撤销状态修改合同信息 金额
     * @param id
     * @param status
     * @param contractList
     * @return
     */
    @Override
    public int updatePaidApplicationSubList(String id, String status, List<Contract> contractList) {
        int updateId = 1;
        updateId = updateCommitPaidAppStatus(id,status);
        if (updateId !=1 ) {
            for(Contract contract:contractList) {  //修改合同主表信息
                contractService.update(contract);            }

        }
        return updateId;
    }

    /**
     * 修改付款申请状态
     * @param id 主表ID
     * @param status 主表状态
     * @return 修改主表的id
     */
    @Override
    public int updateCommitPaidAppStatus(String id, String status){
        PaidApplication paidApplication = selectById(id);
        if("0".equals(status)){ //撤销状态还原
            paidApplication.setAppAmount(0.0);
            paidApplication.setRealAmount(0.0);
        }
        if ("4".equals(status)||"3".equals(status)) {
            paidApplication.setCheckDate(StringUtil.getFormateTime());
        }
        paidApplication.setPaidStatus(status);
        int updateId = update(paidApplication);
        return updateId;
    }

    /**
     * 删除付款申请子表的记录,修改合同表付款申请中的相关记录,
     * 查询付款申请子表申请金额
     * 查询付款申请主表 ,修改主表付款申请金额
     * @param id 主表id
     * @param subIds 子表id
     * @return 修改主表的id
     */
    @Override
    public int deleteOnePaidApplicationSub(String id, List<String> subIds, List<Contract> contractList){
        int deleteId = 1;
        PaidApplicationSub paidApplicationSub = new PaidApplicationSub();
        for (String subId : subIds) {  //删除子表信息
            paidApplicationSub.setId(subId);
            paidApplicationSubService.delete(paidApplicationSub);
        }
        for(Contract contract:contractList) {  //修改合同主表信息
            contractService.update(contract);
        }
        paidApplicationSub.setF_paidId(id);
        HashMap hashMap = paidApplicationSubService.selectTotalByOne(paidApplicationSub);
        Double appBalance = 0.0d;
        PaidApplication paidApplication = selectById(id);
        if(hashMap !=null) {
            if (hashMap.get("APPBALANCE") != null) {
                appBalance = (Double.parseDouble(hashMap.get("APPBALANCE").toString()));
                paidApplication.setAppAmount(appBalance);
            }
        }else {
            paidApplication.setPaidStatus("0");
            paidApplication.setAppAmount(0.0);
        }
        deleteId = update(paidApplication); //修改主表信息
        return deleteId;
    }



}
